package com.feihao.zhou.service.system.Impl;

import com.feihao.zhou.bean.system.Dept;
import com.feihao.zhou.factory.MapperFactory;
import com.feihao.zhou.mapper.system.DeptMapper;
import com.feihao.zhou.service.system.DeptService;
import com.feihao.zhou.utils.TransactionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class DeptServiceImpl implements DeptService {
    @Override
    public void save(Dept dept) {
        SqlSession sqlSession =null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
            //id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString();
            dept.setId(id);
            Integer save = mapper.save(dept);

            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            DeptMapper deptMapper = MapperFactory.getMapper(sqlSession,DeptMapper.class);
            //3.调用Dao层操作
            deptMapper.delete(id);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Dept dept) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            DeptMapper deptMapper = MapperFactory.getMapper(sqlSession,DeptMapper.class);
            //3.调用Dao层操作
            deptMapper.update(dept);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public Dept findById(String id) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            DeptMapper deptDao = MapperFactory.getMapper(sqlSession,DeptMapper.class);
            //3.调用Dao层操作
            return deptDao.findById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Dept> findAll() {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            DeptMapper deptMapper = MapperFactory.getMapper(sqlSession,DeptMapper.class);
            //3.调用Dao层操作
            List<Dept> all = deptMapper.findAll();
            System.out.println(all);

            return all;
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession =null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
            Page<Object> page1 = PageHelper.startPage(page, size);

            List<Dept> all = mapper.findAll();
            System.out.println(all);

            PageInfo pageInfo = new PageInfo(page1);


            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return  null;
    }
}
